//导入数据库
const db=require('../db/index')



exports.sortall= (req,res)=>{
    const sql='select * from sort'
    db.query(sql,(err,results)=>{
        if(err){ return res.cc(err)}
        res.json(results)
    })
}

exports.sortfind= (req,res)=>{
    var info=req.body
    var name=info.name
    var sql= `select * from sort`
    if(info.name.length!=0){
        sql+=` where sort_name like '%${name}%'`
    }
    console.log(sql)
    db.query(sql,(err,results)=>{
        if(err){ return res.cc(err)}
        res.json(results)
    })
}


exports.deletesort= (req,res)=>{
    const sortinfo=req.body
    const sql='delete from sort where sort_id=?'
    db.query(sql,sortinfo.id,(err,results)=>{
        if(err) return res.cc(err)
        if(results.affectedRows!=1)return res.cc('failed to delete')
        res.cc('delete successfully',0)
    })
}

exports.insertsort= (req,res)=>{
    const sortinfo=req.body
    const sql='select * from sort where sort_name=?'
    db.query(sql,sortinfo.sort_name,(err,results)=>{
        if(err) return res.cc(err)
        if(results.length==1) res.cc('sort name is exist,please change new and try again')
        const sql='insert into sort set sort_name= ?'
        db.query(sql,sortinfo.sort_name,(err,results)=>{
        if(err) return res.cc(err)
        if(results.affectedRows!=1)return res.cc('failed to insert')
        res.cc('insert successfully',0)
    })
    })

    
}
exports.updatesort= (req,res)=>{
    const sortinfo=req.body
    const sql='update sort set sort_name=? where sort_id=?'
    db.query(sql,[sortinfo.sort_name,sortinfo.id],(err,results)=>{
        if(err) return res.cc(err)
        if(results.affectedRows!=1)return res.cc('failed to update')
        res.cc('update successfully',0)
    })
}

